package com.ruoyi.framework.web.service;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.framework.manager.factory.AsyncFactory;
import com.ruoyi.framework.security.provider.token.TouristAuthenticationToken;
import com.ruoyi.system.domain.TouristLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>其他方式登录业务层</p>
 * <p>创建日期：2021-07-19</p>
 *
 * @author zhou-qf
 */
@Service
public class SysTouristLoginServiceImpl {

    @Autowired
    private TokenService tokenService;

    @Resource
    private AuthenticationManager authenticationManager;

    public String touristLogin(TouristLogin touristLogin){
        // 进行手动认证授权
        Authentication authentication = authenticationManager
                .authenticate(new TouristAuthenticationToken(touristLogin.getUserCode()));
        AsyncManager.me().execute(AsyncFactory.recordLogininfor(touristLogin.getUserCode(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
        LoginUser loginAppUser = (LoginUser) authentication.getPrincipal();
        // 生成token
        return tokenService.createToken(loginAppUser);
    }
}
